package cn.itsource.xhy.quartz.job;

import cn.itsource.xhy.order.domain.OrderAdopt;
import cn.itsource.xhy.order.domain.OrderProduct;
import cn.itsource.xhy.order.service.IOrderAdoptService;
import cn.itsource.xhy.order.service.IOrderProductService;
import cn.itsource.xhy.pay.contants.BusinessContants;
import cn.itsource.xhy.pay.domain.PayBill;
import cn.itsource.xhy.pay.service.IPayBillService;
import cn.itsource.xhy.pet.domain.Pet;
import cn.itsource.xhy.pet.service.IPetService;
import cn.itsource.xhy.pro.domain.Product;
import cn.itsource.xhy.pro.service.IProductService;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

public class MainJob implements Job {

    @Autowired
    private IPayBillService payBillService;

    @Autowired
    private IOrderAdoptService orderAdoptService;

    @Autowired
    private IOrderProductService orderProductService;

    @Autowired
    private IProductService productService;

    @Autowired
    private IPetService petService;

    //取消订单
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //获取任务
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        //拿到params
        Map<String,Object> params = (Map<String, Object>) jobDataMap.get("params");
        //拿到map里的订单号
        String orderSn = (String) params.get("orderSn");
        //通过订单号查询
        PayBill payBill = payBillService.findByOrderSn(orderSn);
        //将订单状态改为-1
        payBill.setState(-1);
        //修改时间
        payBill.setUpdateTime(new Date());
        //修改数据
        payBillService.update(payBill);

        //通过订单类型做业务
        switch (payBill.getBusinessType()){
            case BusinessContants.ORDER_ADOPT:
                // 领养单
                OrderAdopt orderAdopt = orderAdoptService.findByOrderSn(orderSn);
                // 设置状态
                orderAdopt.setState(-1);
                // 修改
                orderAdoptService.update(orderAdopt);
                //重新上架宠物
                Pet pet = petService.findById(orderAdopt.getPet_id());
                pet.setState(1);
                pet.setOnsaletime(new Date());
                // 修改
                petService.update(pet);
                break;
            case BusinessContants.ORDER_PRODUCT:
                // 服务单
                OrderProduct orderProduct = orderProductService.findByOrderSn(orderSn);
                // 设置状态
                orderProduct.setState(-1);
                // 修改
                orderProductService.update(orderProduct);
                //订单号查询商品销量减一
                Long productId = orderProduct.getProduct_id();
                Product product = productService.findByProId(productId);
                product.setSalecount(product.getSalecount()-1);
                productService.update(product);
                break;
            case BusinessContants.ORDER_GOODS:

                break;
        }

    }
}
